﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FMixPlayer
{
    public partial class FormProjectInfo : Form
    {
        FormMain formParent;

        public FormProjectInfo(FormMain _formParent)
        {
            InitializeComponent();
            formParent = _formParent;
            
            labelName.Text = FormMain.currentProject;

            calculateUsersInfo();

            fillProjectInfo();
        }

        void calculateUsersInfo()
        {
            DataGridViewColumn col = new DataGridViewColumn();
            col.HeaderText = "Сотрудник";
            col.ReadOnly = true;
            DataGridViewColumn col1 = new DataGridViewColumn();
            col1.HeaderText = "Задача";
            col1.ReadOnly = false;
            DataGridViewColumn col2 = new DataGridViewColumn();
            col2.HeaderText = "Стоимость выполнения задачи";
            col2.ReadOnly = false;

            dataGridView1.Columns.Add(col);
            dataGridView1.Columns.Add(col1);
            dataGridView1.Columns.Add(col2);



        }

        void addUserPaymentRow(string _name,string _task,int _cost)
        {
            DataGridViewCell cel1 = new DataGridViewTextBoxCell();
            DataGridViewCell cel2 = new DataGridViewTextBoxCell();
            DataGridViewCell cel3 = new DataGridViewTextBoxCell();
            cel1.Style.BackColor = Color.LightGray;

            
            cel1 = new DataGridViewTextBoxCell();
            cel2 = new DataGridViewTextBoxCell();

            cel3 = new DataGridViewTextBoxCell();

            DataGridViewRow row = new DataGridViewRow();
            cel1.Value = _name;
            cel2.Value = _task;

            cel3.Value = _cost.ToString()+"р.";

            row.Cells.AddRange(cel1, cel2, cel3);
            dataGridView1.Rows.Add(row);
        }
        
        void fillProjectInfo()
        {
            int i = 0;

            DateTime starttime = Convert.ToDateTime(FormMain.Tasks[i].starttime);
            for(i=0;i<=FormMain.Tasks.Count-1;i++){                
                    
                    DateTime storedDate = Convert.ToDateTime(FormMain.Tasks[i].starttime);
                    int result = DateTime.Compare(storedDate, starttime);
                    if (result < 0) starttime=Convert.ToDateTime(FormMain.Tasks[i].starttime);
            }
            labelStartTime.Text = starttime.ToString();
            //
            i = 0;
            DateTime endtime = Convert.ToDateTime(FormMain.Tasks[i].endtime);
            for (i = 0; i <= FormMain.Tasks.Count - 1; i++)
            {
                DateTime storedDate = Convert.ToDateTime(FormMain.Tasks[i].endtime);
                int result = DateTime.Compare(storedDate, endtime);
                if (result > 0) endtime = Convert.ToDateTime(FormMain.Tasks[i].endtime);
            }
            labelEndTime.Text = endtime.ToString();
            //Сколько всего рабочих дней и часов проект идет
            i = 1;
            int totalDays=0;
            TimeSpan totalTime = endtime.Subtract(starttime);
            DateTime tempTime = starttime;
            for (i = 1; i <= totalTime.Days; i++)
            {
                if (tempTime.DayOfWeek == DayOfWeek.Saturday || tempTime.DayOfWeek == DayOfWeek.Sunday)
                {

                }
                else
                {
                    totalDays++;
                }
                tempTime=tempTime.AddDays(1);
            }


            labelTotalDays.Text = totalDays.ToString();
            labelTotalHours.Text = (totalDays*8).ToString();
            //Сколько дней проекта уже прошло
            DateTime now = DateTime.Now;
            TimeSpan timeGone = now.Subtract(starttime);
            tempTime = starttime;
            int goneDays = 0;
            i = 1;
            for (i = 1; i <= timeGone.Days; i++)
            {
                if (tempTime.DayOfWeek == DayOfWeek.Saturday || tempTime.DayOfWeek == DayOfWeek.Sunday)
                {

                }
                else
                {
                    goneDays++;
                }
                tempTime = tempTime.AddDays(1);
            }

            double gonePercents;
            gonePercents = (100 / totalDays) * goneDays;
            labelTimeGone.Text = Math.Round(gonePercents).ToString()+"%";
            //Задачи проекта завершены
            i = 0;
            int tasksCompleted = 0;
            for (i = 0; i <= FormMain.Tasks.Count - 1; i++)
            {
                if (FormMain.Tasks[i].status == "Выполнено") tasksCompleted++;
            }
            labelPercentsComplete.Text = Math.Round((((float)100 / (float)FormMain.Tasks.Count) * (float)tasksCompleted)).ToString()+"%";
            
            //Всего людей работало над проектом
            i = 0;
            
            
            List<string> peoples = new List<string>();
            List<string> tempList = new List<string>();
            for (i = 0; i <= FormMain.Tasks.Count - 1; i++)
            {
                peoples.Add(FormMain.Tasks[i].executor);
                tempList.Add(FormMain.Tasks[i].executor);

            }
            i = 0;


            List<string> WorkingUsers = new List<string>();

            for (i = 0; i <= peoples.Count - 1; i++)
            {
                int z = 0;
                for (int j = 0; j <= tempList.Count - 1; j++)
                {

                    if (peoples[i] == tempList[j] && tempList[j] != "!")
                    {
                        tempList[j] = "!";
                        z++;
                    }
                }

                if (z > 0)
                {
                    
                    WorkingUsers.Add(peoples[i]);
                }
            }
            labelPeoples.Text = WorkingUsers.Count.ToString();
            //
            i = 0;
            int temptemp = 0;
            
            int cost = 0;

            for (i = 0; i <= FormMain.Tasks.Count - 1; i++)
            {
                double hours = 0;
                
                DateTime startTimeTask = Convert.ToDateTime(FormMain.Tasks[i].starttime);
                DateTime endTimeTask = Convert.ToDateTime(FormMain.Tasks[i].endtime);
                TimeSpan timeTaskTotal = endTimeTask.Subtract(startTimeTask);
                hours = timeTaskTotal.TotalDays * 8;
                for(int j=0;j<=FormMain.Users.Count-1;j++){
                    if (FormMain.Tasks[i].executor == FormMain.Users[j].login)
                    {
                        cost += Convert.ToInt16(hours) * (FormMain.Users[j].payment / 160);
                        temptemp++;
                        addUserPaymentRow(FormMain.Users[j].login, FormMain.Tasks[i].name, Convert.ToInt16(hours) * (FormMain.Users[j].payment / 160));
                    }
                }
                
            }
            labelTotalCost.Text = cost.ToString()+"р.";
            //
            i = 0;
            
        }

 
    }
}
